<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品分类" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入商品分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:storeProduct:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:storeProduct:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:storeProduct:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:storeProduct:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeProductList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商品id" align="center" prop="id" v-if="true"/>
      <el-table-column label="商品图片" align="center" prop="fileName" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.fileName" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="storeName"/>
      <!--      <el-table-column label="商品简介" align="center" prop="storeInfo" />-->
      <!--      <el-table-column label="关键字" align="center" prop="keyword" />-->
      <!--      <el-table-column label="商品条码" align="center" prop="barCode" />-->
      <!--      <el-table-column label="分类id" align="center" prop="cateId" />-->
      <el-table-column label="商品价格" align="center" prop="price"/>
      <!--      <el-table-column label="会员价格" align="center" prop="vipPrice" />-->
      <!--      <el-table-column label="市场价" align="center" prop="otPrice" />-->
      <!--      <el-table-column label="邮费" align="center" prop="postage" />-->
      <!--      <el-table-column label="单位名" align="center" prop="unitName" />-->
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="销量" align="center" prop="sales"/>
      <el-table-column label="库存" align="center" prop="stock"/>
      <el-table-column label="是否上架" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!--      <el-table-column label="是否热卖" align="center" prop="isHot" />-->
      <!--      <el-table-column label="是否优惠" align="center" prop="isBenefit" />-->
      <!--      <el-table-column label="是否精品" align="center" prop="isBest" />-->
      <!--      <el-table-column label="是否新品" align="center" prop="isNew" />-->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="是否包邮" align="center" prop="isPostage" />-->
      <!--      <el-table-column label="商户是否代理 0不可代理1可代理" align="center" prop="merUse" />-->
      <!--      <el-table-column label="获得积分" align="center" prop="giveIntegral" />-->
      <!--      <el-table-column label="成本价" align="center" prop="cost" />-->
      <!--      <el-table-column label="秒杀状态 0 未开启 1已开启" align="center" prop="isSeckill" />-->
      <!--      <el-table-column label="砍价状态 0未开启 1开启" align="center" prop="isBargain" />-->
      <!--      <el-table-column label="是否优品推荐" align="center" prop="isGood" />-->
      <!--      <el-table-column label="是否单独分佣" align="center" prop="isSub" />-->
      <!--      <el-table-column label="虚拟销量" align="center" prop="ficti" />-->
      <!--      <el-table-column label="浏览量" align="center" prop="browse" />-->
      <!--      <el-table-column label="商品二维码地址(用户小程序海报)" align="center" prop="codePath" />-->
      <!--      <el-table-column label="淘宝京东1688类型" align="center" prop="soureLink" />-->
      <!--      <el-table-column label="主图视频链接" align="center" prop="videoLink" />-->
      <!--      <el-table-column label="运费模板ID" align="center" prop="tempId" />-->
      <!--      <el-table-column label="规格 0单 1多" align="center" prop="specType" />-->
      <!--      <el-table-column label="活动显示排序0=默认, 1=秒杀，2=砍价，3=拼团" align="center" prop="activity" />-->
      <!--      <el-table-column label="展示图" align="center" prop="flatPattern" />-->
      <!--      <el-table-column label="是否回收站" align="center" prop="isRecycle" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:storeProduct:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:storeProduct:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品分类" prop="cateId">
          <el-cascader v-model="form.cateId" :options="merCateList" :props="props2"
                       :style="{width: '100%'}" placeholder="请选择级联选择" clearable></el-cascader>
        </el-form-item>
        <el-form-item label="商品图片">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>
        <el-form-item label="轮播图">
          <image-upload v-model="form.sliderImage"/>
        </el-form-item>
        <el-form-item label="商品简介" prop="storeInfo">
          <el-input v-model="form.storeInfo" placeholder="请输入商品简介"/>
        </el-form-item>
        <el-form-item label="商品详情">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select v-model="form.standardId" placeholder="商品规格" @change="changeObj">
            <el-option
              v-for="dict in standard"
              :key="dict.id"
              :label="dict.ruleName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规格属性" v-if="isFlag">
          <SkuForm
            ref="skuForm"
            :source-attribute="sourceAttribute"
            :attribute.sync="attribute"
            :sku.sync="sku"
            :structure="structure"
          >
            <template #score="slotProps">
              <div>
                <el-rate v-model="slotProps.row.score"/>
              </div>
            </template>
            <template #image="slotProps">
              <div>
                <image-upload class="img-size" v-model="slotProps.row.image" :limit="1"/>
              </div>
            </template>
          </SkuForm>
        </el-form-item>
        <el-form-item label="邮费" prop="postage">
          <el-input-number v-model="form.postage" placeholder="请输入邮费"/>
        </el-form-item>
        <el-form-item label="单位名" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="销量" prop="sales">
          <el-input v-model="form.sales" placeholder="请输入销量"/>
        </el-form-item>
<!--        <el-form-item label="运费" prop="stock">-->
<!--          <el-input v-model="form.stock" placeholder="请输入运费"/>-->
<!--        </el-form-item>-->
        <el-form-item label="是否热卖" prop="isHot">
          <el-select v-model="form.isHot" placeholder="请输入是否热卖">
            <el-option
              v-for="dict in dict.type.common_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="是否优惠" prop="isBenefit">-->
        <!--          <el-select v-model="form.isBenefit" placeholder="请输入是否优惠">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.common_type"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="是否精品" prop="isBest">
          <el-select v-model="form.isBest" placeholder="请输入是否精品">
            <el-option
              v-for="dict in dict.type.common_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品" prop="isNew">
          <el-select v-model="form.isNew" placeholder="请输入是否新品">
            <el-option
              v-for="dict in dict.type.common_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否优品推荐" prop="isGood">
          <el-select v-model="form.isGood" placeholder="请输入是否优品推荐">
            <el-option
              v-for="dict in dict.type.common_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="是否包邮" prop="isPostage">-->
        <!--          <el-select v-model="form.isPostage" placeholder="请输入是否包邮">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.common_type"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="获得积分" prop="giveIntegral">
          <el-input v-model="form.giveIntegral" placeholder="请输入获得积分"/>
        </el-form-item>
        <el-form-item label="成本价" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入成本价"/>
        </el-form-item>
        <el-form-item label="秒杀状态" prop="isSeckill">
          <el-select v-model="form.isSeckill" placeholder="请输入秒杀状态">
            <el-option
              v-for="dict in dict.type.common_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="选择优惠券" prop="isSeckill">-->
        <!--          <el-select v-model="form.couponIds" placeholder="选择优惠券">-->
        <!--            <el-option-->
        <!--              v-for="dict in coupons"-->
        <!--              :key="dict.id"-->
        <!--              :label="dict.name"-->
        <!--              :value="dict.id"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->

        <el-form-item label="优惠券：" class="proCoupon">
          <div class="acea-row">
            <el-tag
              v-for="(tag, index) in form.coupons"
              :key="index"
              class="mr10 mb10"
            >
              {{ tag.name }}
            </el-tag>
            <el-button v-if="!isDisabled" class="mr15" @click="addCoupon">选择优惠券</el-button>
          </div>
        </el-form-item>
        <!--        <el-form-item label="是否单独分佣" prop="isSub">-->
        <!--          <el-input v-model="form.isSub" placeholder="请输入是否单独分佣" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="虚拟销量" prop="ficti">-->
        <!--          <el-input v-model="form.ficti" placeholder="请输入虚拟销量" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="浏览量" prop="browse">-->
        <!--          <el-input v-model="form.browse" placeholder="请输入浏览量" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="商品二维码地址(用户小程序海报)" prop="codePath">-->
        <!--          <el-input v-model="form.codePath" placeholder="请输入商品二维码地址(用户小程序海报)" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="淘宝京东1688类型" prop="soureLink">-->
        <!--          <el-input v-model="form.soureLink" placeholder="请输入淘宝京东1688类型" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="主图视频链接" prop="videoLink">-->
        <!--          <el-input v-model="form.videoLink" placeholder="请输入主图视频链接" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="运费模板ID" prop="tempId">-->
        <!--          <el-input v-model="form.tempId" placeholder="请输入运费模板ID" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="活动显示排序0=默认, 1=秒杀，2=砍价，3=拼团" prop="activity">-->
        <!--          <el-input v-model="form.activity" placeholder="请输入活动显示排序0=默认, 1=秒杀，2=砍价，3=拼团" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="展示图" prop="flatPattern">-->
        <!--          <el-input v-model="form.flatPattern" type="textarea" placeholder="请输入内容" />-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择优惠券" :visible.sync="couponOpen" width="800px" append-to-body>
      <el-form :model="queryCouponParams" ref="queryCouponForm" size="small" :inline="true" v-show="showSearch"
               label-width="68px">
        <el-form-item label="优惠券名称" prop="name">
          <el-input
            v-model="queryCouponParams.name"
            placeholder="请输入优惠券名称"
            clearable
            @keyup.enter.native="handleCouponQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleCouponQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="couponLoad" :data="coupons" @selection-change="handleSelectionCouponChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="ID" align="center" prop="id" v-if="true"/>
        <el-table-column label="优惠券名称" align="center" prop="name"/>
        <el-table-column label="优惠券面值" align="center" prop="money"/>
        <el-table-column label="剩余数量" align="center" prop="lastTotal"/>
        <el-table-column label="最低消费" align="center" prop="minPrice"/>
      </el-table>

      <pagination
        v-show="couponTotal>0"
        :total="couponTotal"
        :page.sync="queryCouponParams.pageNum"
        :limit.sync="queryCouponParams.pageSize"
        @pagination="getCouponList"
      />

      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitCouponForm">确 定</el-button>-->
        <el-button @click="cancelCoupon">确 认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listStoreProduct,
  getStoreProduct,
  delStoreProduct,
  addStoreProduct,
  updateStoreProduct, changeStatus
} from "@/api/system/storeProduct";
import {listCategory} from "@/api/system/category";
import {listStoreProductRule} from "@/api/system/storeProductRule";
import {listAskCoupon} from "@/api/system/askCoupon";
import {changeRoleStatus} from "@/api/system/role";

export default {
  name: "StoreProduct",
  dicts: ['common_type'],
  data() {
    return {
      // 购物券开关
      couponOpen: false,
      isDisabled: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      storeProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryCouponParams: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merId: undefined,
        image: undefined,
        sliderImage: undefined,
        storeName: undefined,
        storeInfo: undefined,
        keyword: undefined,
        barCode: undefined,
        cateId: undefined,
        price: undefined,
        vipPrice: undefined,
        otPrice: undefined,
        postage: undefined,
        unitName: undefined,
        sort: undefined,
        sales: undefined,
        stock: undefined,
        isShow: undefined,
        isHot: undefined,
        isBenefit: undefined,
        isBest: undefined,
        isNew: undefined,
        addTime: undefined,
        isPostage: undefined,
        merUse: undefined,
        giveIntegral: undefined,
        cost: undefined,
        isSeckill: undefined,
        isBargain: undefined,
        isGood: undefined,
        isSub: undefined,
        ficti: undefined,
        browse: undefined,
        codePath: undefined,
        soureLink: undefined,
        videoLink: undefined,
        tempId: undefined,
        specType: undefined,
        activity: undefined,
        flatPattern: undefined,
        isRecycle: undefined
      },
      // 表单参数
      form: {},
      merCateList: [], // 商户分类筛选
      props2: {
        children: 'children',
        label: 'name',
        value: 'id',
        multiple: false,
        emitPath: false
      },
      // 表单校验
      rules: {
        id: [
          {required: true, message: "商品id不能为空", trigger: "blur"}
        ],
        merId: [
          {required: true, message: "商户Id(0为总后台管理员创建,不为0的时候是商户后台创建)不能为空", trigger: "blur"}
        ],
        image: [
          {required: true, message: "商品图片不能为空", trigger: "blur"}
        ],
        sliderImage: [
          {required: true, message: "轮播图不能为空", trigger: "blur"}
        ],
        storeName: [
          {required: true, message: "商品名称不能为空", trigger: "blur"}
        ],
        storeInfo: [
          {required: true, message: "商品简介不能为空", trigger: "blur"}
        ],
        keyword: [
          {required: true, message: "关键字不能为空", trigger: "blur"}
        ],
        barCode: [
          {required: true, message: "商品条码不能为空", trigger: "blur"}
        ],
        cateId: [
          {required: true, message: "分类id不能为空", trigger: "blur"}
        ],
        price: [
          {required: true, message: "商品价格不能为空", trigger: "blur"}
        ],
        vipPrice: [
          {required: true, message: "会员价格不能为空", trigger: "blur"}
        ],
        otPrice: [
          {required: true, message: "市场价不能为空", trigger: "blur"}
        ],
        postage: [
          {required: true, message: "邮费不能为空", trigger: "blur"}
        ],
        unitName: [
          {required: true, message: "单位名不能为空", trigger: "blur"}
        ],
        sort: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ],
        sales: [
          {required: true, message: "销量不能为空", trigger: "blur"}
        ],
        stock: [
          {required: true, message: "库存不能为空", trigger: "blur"}
        ],
        isShow: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        isHot: [
          {required: true, message: "是否热卖不能为空", trigger: "blur"}
        ],
        isBenefit: [
          {required: true, message: "是否优惠不能为空", trigger: "blur"}
        ],
        isBest: [
          {required: true, message: "是否精品不能为空", trigger: "blur"}
        ],
        isNew: [
          {required: true, message: "是否新品不能为空", trigger: "blur"}
        ],
        addTime: [
          {required: true, message: "添加时间不能为空", trigger: "blur"}
        ],
        isPostage: [
          {required: true, message: "是否包邮不能为空", trigger: "blur"}
        ],
        merUse: [
          {required: true, message: "商户是否代理 0不可代理1可代理不能为空", trigger: "blur"}
        ],
        giveIntegral: [
          {required: true, message: "获得积分不能为空", trigger: "blur"}
        ],
        cost: [
          {required: true, message: "成本价不能为空", trigger: "blur"}
        ],
        isSeckill: [
          {required: true, message: "秒杀状态 0 未开启 1已开启不能为空", trigger: "blur"}
        ],
        isBargain: [
          {required: true, message: "砍价状态 0未开启 1开启不能为空", trigger: "blur"}
        ],
        isGood: [
          {required: true, message: "是否优品推荐不能为空", trigger: "blur"}
        ],
        isSub: [
          {required: true, message: "是否单独分佣不能为空", trigger: "blur"}
        ],
        ficti: [
          {required: true, message: "虚拟销量不能为空", trigger: "blur"}
        ],
        browse: [
          {required: true, message: "浏览量不能为空", trigger: "blur"}
        ],
        codePath: [
          {required: true, message: "商品二维码地址(用户小程序海报)不能为空", trigger: "blur"}
        ],
        soureLink: [
          {required: true, message: "淘宝京东1688类型不能为空", trigger: "blur"}
        ],
        videoLink: [
          {required: true, message: "主图视频链接不能为空", trigger: "blur"}
        ],
        tempId: [
          {required: true, message: "运费模板ID不能为空", trigger: "blur"}
        ],
        specType: [
          {required: true, message: "规格 0单 1多不能为空", trigger: "change"}
        ],
        flatPattern: [
          {required: true, message: "展示图不能为空", trigger: "blur"}
        ],
        isRecycle: [
          {required: true, message: "是否回收站不能为空", trigger: "blur"}
        ]
      },
      sourceAttribute: [],
      attribute: [],
      sku: [],
      structure: [
        {name: 'image', type: 'slot', label: '图片'},
        {name: 'price', type: 'input', label: '价格'},
        {name: 'cost', type: 'input', label: '成本价'},
        {name: 'otPrice', type: 'input', label: '原价'},
        {name: 'stock', type: 'input', label: '库存'},
        {name: 'weight', type: 'input', label: '重量'},
        {name: 'volume', type: 'input', label: '体积'}
      ],
      standard: [],
      isFlag: false,
      coupons: [],
      couponTotal: null,
      couponLoad: false
    };
  },
  created() {
    this.getList();
    this.getCategoryList()
    this.getAttrList()
    this.getCouponList()
  },
  methods: {
    // 上架状态修改
    handleStatusChange(row) {
      // let text = row.status === "0" ? "下架" : "上架";
      this.$modal.confirm('确认要修改上架状态吗').then(function () {
        return changeStatus(row.id, row.isShow);
      }).then(() => {
        this.$modal.msgSuccess("成功");
      }).catch(function () {
        row.isShow = row.isShow === "0" ? "1" : "0";
      });
    },
    addCoupon() {
      this.couponOpen = true
    },
    // 获取规格列表
    getCouponList() {
      this.couponLoad = true
      this.queryCouponParams.userType = 2;
      listAskCoupon(this.queryCouponParams).then(res => {
        this.coupons = res.rows
        this.couponTotal = res.total;
        this.couponLoad = false;
      });
    },
    // 获取规格列表
    getAttrList() {
      listStoreProductRule().then(res => {
        this.standard = res.rows
      });
    },
    // 下拉更改
    changeObj() {
      this.sourceAttribute = []
      const data = {
        id: this.form.standardId
      }

      listStoreProductRule(data).then(res => {
        res.rows.forEach(e => {
          const value = JSON.parse(e.ruleValue)
          value.forEach(e => {
            this.sourceAttribute.push({
              name: e.value,
              item: e.detail
            })
          })
        })

        this.isFlag = true
        this.$refs.skuForm.init()
      });

    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listStoreProduct(this.queryParams).then(response => {
        this.storeProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询分类
    getCategoryList() {
      this.loading = true;
      listCategory().then(response => {
        this.merCateList = this.handleTree(response.data, "id", "pid");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.isFlag = false;
      this.reset();
    },
    // 购物券取消
    cancelCoupon() {
      this.couponOpen = false;
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        merId: undefined,
        image: undefined,
        sliderImage: undefined,
        storeName: undefined,
        storeInfo: undefined,
        keyword: undefined,
        barCode: undefined,
        cateId: undefined,
        price: undefined,
        vipPrice: undefined,
        otPrice: undefined,
        postage: undefined,
        unitName: undefined,
        sort: undefined,
        sales: undefined,
        stock: undefined,
        isShow: undefined,
        isHot: undefined,
        isBenefit: undefined,
        isBest: undefined,
        isNew: undefined,
        addTime: undefined,
        isPostage: undefined,
        merUse: undefined,
        giveIntegral: undefined,
        cost: undefined,
        isSeckill: undefined,
        isBargain: undefined,
        isGood: undefined,
        isSub: undefined,
        ficti: undefined,
        browse: undefined,
        codePath: undefined,
        soureLink: undefined,
        videoLink: undefined,
        tempId: undefined,
        specType: undefined,
        activity: undefined,
        flatPattern: undefined,
        isRecycle: undefined,
        standardId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleCouponQuery() {
      this.queryCouponParams.pageNum = 1;
      this.getCouponList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleSelectionCouponChange(selection) {
      this.form.coupons = []
      selection.forEach(e => {
        this.form.coupons.push(e);
      })

      this.form.couponIds = selection.map(item => item.id)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getStoreProduct(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.changeObj()
        this.sku = this.form.attrValue
        this.attribute = this.form.attr
        this.isFlag = true
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.attrValue = this.sku
      this.form.attr = this.attribute
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateStoreProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStoreProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.form.couponIds = []
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delStoreProduct(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/storeProduct/export', {
        ...this.queryParams
      }, `storeProduct_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
/deep/ .img-size {
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
  border-radius: 6px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 100px;
  height: 100px;
  cursor: pointer;
  vertical-align: top;
}
</style>
